﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.DataVisualization.Charting;

namespace WebApplication.Controllers
{
    public class WorkController : Controller
    {
        Models.DataClasses1DataContext d = new Models.DataClasses1DataContext();
        // GET: Work
        public ActionResult Index()
        {
            return View(d.Work);
        }

 

        // GET: Work/Create
        public ActionResult Create()
        {
            return View(d.DataSet.ToList());
        }

        // POST: Work/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
             
                Models.Work set = new Models.Work()
                {
                    name = collection.GetValues("name")[0],
                    note = collection.GetValues("note")[0],
                    param = collection.GetValues("method")[0]+";"+ collection.GetValues("hiden")[0]+";" + collection.GetValues("output")[0] + ";" + collection.GetValues("Layers")[0] + ";" + collection.GetValues("max")[0] + ";" + collection.GetValues("step")[0] + ";",
                    startTime=DateTime.Now,
                    state  = collection.GetValues("start")[0]=="true"?1 :0,
                 idDataSet = Convert.ToInt32(collection.GetValues("idDataSet")[0]),

                };

                d.Work.InsertOnSubmit(set);
                d.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return View();
            }
        }
        public FileContentResult GetChart(int id)
        {
            List<PointF> points = new List<PointF>();

            foreach (var p in d.Temp.Where(t=>t.idWork==id))
            {
                points.Add(new PointF(p.stap,(float)Convert.ToDouble( p.test)));
            }
            var chart = new Chart();
            chart.Width = 1000;
            chart.Height = 500;
            chart.BackColor = Color.White;
            chart.BorderlineDashStyle = ChartDashStyle.NotSet;
            chart.BackSecondaryColor = Color.White;
            chart.BackGradientStyle = GradientStyle.TopBottom;
            chart.BorderlineWidth = 0;
       
            chart.RenderType = RenderType.BinaryStreaming;
            chart.BorderSkin.SkinStyle = BorderSkinStyle.None;
            chart.AntiAliasing = AntiAliasingStyles.All;
            chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;

            chart.Series.Add(CreateSeries(points, SeriesChartType.Line, Color.Red));
            chart.ChartAreas.Add(CreateChartArea());

            var ms = new MemoryStream();
            chart.SaveImage(ms);
            return File(ms.GetBuffer(), @"image/png");
        }


        [NonAction]
        public Series CreateSeries(List<PointF> results,
       SeriesChartType chartType,
       Color color)
        {
            var seriesDetail = new Series();
            seriesDetail.Name = "Errors";
            seriesDetail.IsValueShownAsLabel = false;
            seriesDetail.Color = color;
            seriesDetail.ChartType = chartType;
            seriesDetail.BorderWidth = 2;
            seriesDetail["DrawingStyle"] = "Cylinder";
            seriesDetail["PieDrawingStyle"] = "SoftEdge";
            DataPoint point;
         
            foreach (var result in results)
            {
                seriesDetail.Points.AddXY(result.X, result.Y);
            }
            seriesDetail.ChartArea = "Errors";

            return seriesDetail;
        }
      
        public ChartArea CreateChartArea()
        {
            var chartArea = new ChartArea();
            chartArea.Name = "Errors";
            chartArea.BackColor = Color.Transparent;

            return chartArea;
        }

        // GET: Work/Edit/5
        public ActionResult Edit(int id)
        {

            return View(d.Work.First(t=>t.id==id));
        }

        // POST: Work/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var v = d.Work.First(t => t.id == id);
                v.name = collection.GetValues("name")[0];
                v.note = collection.GetValues("note")[0];
                v.state=Convert.ToInt32( collection.GetValues("state")[0]);
                d.SubmitChanges();


                return RedirectToAction("Index");

             
            }
            catch
            {
                return View();
            }
        }

        // GET: Work/Delete/5
        public ActionResult Delete(int id)
        {
            d.Work.DeleteAllOnSubmit(d.Work.Where(t => t.id == id));
            d.SubmitChanges();

            return View("Index");
        }

        // POST: Work/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return Delete(id);
            }
            catch
            {
                return View("Index");
            }
        }
    }
}
